package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.atguigu.jxc.service.LogService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {
    @Autowired
    DamageListGoodsDao damageListGoodsDao;
    @Autowired
    LogService logService;
    @Autowired
    UserDao userDao;
    @Autowired
    GoodsDao goodsDao;

    //保存报损单
    @Override
    public ServiceVO saveDamageListGoods(DamageList damageList, String damageListGoodsStr) {
        // 使用谷歌Gson将JSON字符串数组转换成具体的集合
        Gson gson = new Gson();

        List<DamageListGoods> damageListGoods = gson.fromJson(damageListGoodsStr,new TypeToken<List<DamageListGoods>>(){}.getType());

        // 设置当前操作用户
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());

        damageList.setUserId(currentUser.getUserId());

        // 保存报损单信息
        damageListGoodsDao.saveDamageList(damageList);

        // 保存报损单商品信息
        for (DamageListGoods damageListGood : damageListGoods) {
            damageListGood.setDamageListId(damageList.getDamageListId());

            damageListGoodsDao.saveDamageListGoods(damageListGood);

            // 修改商品库存，状态
            Goods goods = goodsDao.findByGoodsId(damageListGood.getGoodsId());

            goods.setInventoryQuantity(goods.getInventoryQuantity()-damageListGood.getGoodsNum());

            goods.setState(2);

            goodsDao.updateGoods(goods);
        }

        // 保存日志
        logService.save(new Log(Log.INSERT_ACTION, "新增报损单："+damageList.getDamageNumber()));

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    //报损单查询
    @Override
    public Map<String, Object> getDamageList(String sTime, String eTime) {
        Map<String,Object> map = new HashMap<>();

        List<DamageList> list = damageListGoodsDao.getDamageList(sTime,eTime);

        logService.save(new Log(Log.SELECT_ACTION,"查询报损单商品信息"));

        map.put("rows", list);

        return map;
    }

    //查询报损单商品信息
    @Override
    public Map<String, Object> getDamageGoodsList(Integer damageListId) {
        Map<String,Object> map = new HashMap<>();

        if (!StringUtils.isEmpty(damageListId)){

            List<DamageListGoods> list = damageListGoodsDao.getDamageGoodsList(damageListId);
            logService.save(new Log(Log.SELECT_ACTION,"查询报损单商品信息"));

            map.put("rows", list);
        }


        return map;
    }
}
